/**   
 * @Title AutoEnt.java
 * @package com.jinxinol.lis.util
 * @Description: TODO
 * @author tuxy,wangrong
 * @date Jul 17, 2012 5:03:52 PM
 * @version V1.1
 */
package com.jinxinol.core.util.autosrc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Date;

import com.jinxinol.core.util.StringUtils;

/**
 * 按数据库的表结构生成java实体类
 * @author tuxy
 * @time Jul 17, 2012 5:03:52 PM
 */
public class AutoEnt {
	//---------------------------------------------- 生成配置文件-BEGIN ----------------------------------------------------------/
	//使用说明:按实际需求修改以下配置路径
	//自动文件生成路径。
	private static String filePath = "c://jxis";
	//表名匹配符  .此配置是作用是：匹配对应表才自动生成代码
	private static String table_Compare="BSS_%";
	//文件包路径
	private static String pagPath  = "com.jinxinol.jxis.bss.basicdata";
	//公共包路径
	private static String commonPath = "com.jinxinol.core.common";
	//数据库连接URL
	private static String CONN_URL = "jdbc:oracle:thin:@192.168.30.161:1521:xe";
	//数据库驱动类
	private static String CONN_DRIVER_CLASS="oracle.jdbc.driver.OracleDriver";
	//数据库连接用户名
	private static String CONN_USER="jxis2";
	//数据库连接密码
	private static String CONN_PASSWORD="jxis2";
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//---------------------------------------------- 生成配置文件-END ----------------------------------------------------------/
	//线程安全
	public static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
	
	
	private static String pagPathJ = pagPath+".domain";
	private static String pagPathM = pagPath+".mapper";
	private static String pagPathS = pagPath+".service";
	private static String pagPathSI = pagPath+".service.impl";
	private static String pagPathWeb = pagPath+".web";
	
	private static String imp = "import "+commonPath+".IMapper;";
	private static String impS = "import "+commonPath+".IService;";
	private static String impAS = "import "+commonPath+".AbstractService;";
	private static String impPM = "import "+commonPath+".PageModel;";
	private static String impBAction = "import "+commonPath+".BaseAction;";
	private static String impContextHelper = "import "+commonPath+".ContextHelper;";
	private static String imp_entity = "import "+commonPath+".Entity;";
	
	private static String tablename;
	private static String tabCom;
	private static String[] colnames; // 列名数组
	private static String[] colTypes; // 列名类型数组
	private static String[] colComs; // 列备注数组
	private static String serviceName;
	private static String serviceImplName;
	private static String entityName;
	private static String mapperName;
	private static String actionName;
	
	private static boolean f_util = true; // 是否需要导入包java.util.*
	private static boolean f_sql = false; // 是否需要导入包java.sql.*
	private static boolean f_date_to_str = false;  //数据库中的DATE类型是否要转换成JAVA中的STRING类型。默认转为JAVA中的DATE类型;
	private static boolean f_ext_entity = true;
	
	public static void main(String[] args) throws Exception{
		String fp1 = filePath + "//domain";
		String fp2 = filePath + "//mapper";
		String fp3 = filePath + "//service";
		String fp4 = filePath + "//service//impl";
		String fp5 = filePath + "//web";
		//创建生成目录
		File tempFile = new File(filePath);
		tempFile = new File(fp1);
		tempFile.mkdirs();
		tempFile = new File(fp2);
		tempFile.mkdirs();
		tempFile = new File(fp3);
		tempFile.mkdirs();
		tempFile = new File(fp4);
		tempFile.mkdirs();
		tempFile = new File(fp5);
		tempFile.mkdirs();
		
		PreparedStatement pstm = null;
		ResultSet rs = null;
		Connection conn = null;
		try{
			conn = getConn(); // 得到数据库连接
			String sql = "select table_name from user_tables t where t.TABLE_NAME like '"+table_Compare+"'";  //查询当前用户所拥有的表名
//			String sql = "select table_name from user_tables t where t.TABLE_NAME in ('BSS_COMM_BIZ_HIS','BSS_COMM_CATE','BSS_COMM_ITEM','BSS_DEPOT_COMM_TJ','BSS_PRICE_ADJUST')";  //查询当前用户所拥有的表名
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			StringBuffer sb = new StringBuffer();
			sb.append("<!--\r\n\r\n");
			while(rs.next()){
				tablename = rs.getString(1);
				foundJava();
				sb.append("\t\t<typeAlias alias=\""+ initcap(tablename)+"\" type=\""+ pagPathJ +"."+ initcap(tablename)+"\"/>\r\n\r\n");
			}
			sb.append("-->");
			String content = sb.toString();
			System.out.println("创建配置文件。");
			String path = filePath+"//config.xml";
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8"); 
			PrintWriter pw = new PrintWriter(out);
			pw.println(content);
			pw.flush();
			pw.close();
		}finally{
			release(pstm, rs, conn);
		}
	} 
	/** 
	 * <p>说明：按表名生成java实体类</p>
	 * @param tbn
	 * @throws Exception
	 * @return void
	 * @author tuxy
	 * @date Jul 17, 2012 6:09:02 PM
	 */ 
	public static void foundJava() throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String strsql = "select * from " + tablename;
		String strsql2 = "SELECT COMMENTS FROM USER_TAB_COMMENTS WHERE TABLE_NAME = '"+ tablename +"'"; //查询表注释
		String strsql3 = "SELECT COLUMN_NAME, COMMENTS FROM USER_COL_COMMENTS WHERE TABLE_NAME = '"+ tablename +"'"; //查询行注释
		try {
			conn = getConn(); // 得到数据库连接
			pstmt = conn.prepareStatement(strsql);
			pstmt.execute();
			ResultSetMetaData rsmd = pstmt.getMetaData();
			
			pstmt = conn.prepareStatement(strsql2);
			pstmt.execute();
			rs = pstmt.executeQuery();
			tabCom ="无";
			if(rs.next()){
				tabCom= rs.getString(1);
			}
			
			pstmt = conn.prepareStatement(strsql3);
			pstmt.execute();
			rs = pstmt.executeQuery();
			int size = rsmd.getColumnCount(); // 共有多少列
			colComs = new String[size];
			colnames = new String[size];
			colTypes = new String[size];
			entityName = initcap(tablename);
			serviceName = "I"+entityName+"Service";
			serviceImplName = entityName+"Service";
			mapperName = entityName+"Mapper";
			actionName = entityName+"Action";
			
			for (int i = 0; i < rsmd.getColumnCount(); i++) {
				colnames[i] = rsmd.getColumnName(i + 1);
				colTypes[i] = rsmd.getColumnTypeName(i + 1);
				boolean flag = true;
				while(rs.next()){
					if(rs.getString(1).equals(rsmd.getColumnName(i + 1))){
						colComs[i] = rs.getString(2);
						flag = false;
						break;
					}
				}
				if(flag){
					colComs[i] = "无";
				}
				if (colTypes[i].equalsIgnoreCase("datetime")) {
					f_util = true;
				}
				if (colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")) {
					f_sql = true;
				}
			}
			String content = parse();
			String contentMapper = creatMapper();
			String contentMapperXml = creatMapperXml();
			String contentService = creatService();
			String contentServiceImpl = creatServiceImpl();
			String contentAction  = createAction();
			try {
				System.out.println("创建："+initcap(tablename));
				String path = filePath+"//domain//"+initcap(tablename) + ".java";
				String pathM = filePath+"//mapper//"+initcap(tablename) + "Mapper.java";
				String pathMX = filePath+"//mapper//"+initcap(tablename) + "Mapper.xml";
				String pathS = filePath+"//service//"+serviceName+".java";
				String pathSI = filePath+"//service//impl//"+ serviceImplName + ".java";
				String pathWeb = filePath+"//web//"+actionName+".java";
				OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8"); 
				PrintWriter pw = new PrintWriter(out);
				pw.println(content);
				pw.flush();
				pw.close();
				out = new OutputStreamWriter(new FileOutputStream(pathM),"UTF-8"); 
				pw = new PrintWriter(out);
				pw.println(contentMapper);
				pw.flush();
				pw.close();
				out = new OutputStreamWriter(new FileOutputStream(pathMX),"UTF-8"); 
				pw = new PrintWriter(out);
				pw.println(contentMapperXml);
				pw.flush();
				pw.close();
				out = new OutputStreamWriter(new FileOutputStream(pathS),"UTF-8"); 
				pw = new PrintWriter(out);
				pw.println(contentService);
				pw.flush();
				pw.close();
				out = new OutputStreamWriter(new FileOutputStream(pathSI),"UTF-8"); 
				pw = new PrintWriter(out);
				pw.println(contentServiceImpl);
				pw.flush();
				pw.close();
				out = new OutputStreamWriter(new FileOutputStream(pathWeb),"UTF-8"); 
				pw = new PrintWriter(out);
				pw.println(contentAction);
				pw.flush();
				pw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			release(pstmt, rs, null);
		}
	}
	
	/** 
	 * <p>说明：生成mapper的java代码</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 10:11:43 AM
	 */ 
	private static String creatMapper(){
		StringBuffer sb = new StringBuffer();
		sb.append("package "+pagPathM+";\r\n\r\n");
		sb.append(imp+"\r\n\r\n");
		sb.append("import "+ pagPathJ +"."+ initcap(tablename)+";\r\n\r\n");
		sb.append("/**\r\n");
		sb.append(" * "+ tabCom+ "\r\n");
		sb.append(" */\r\n");
		sb.append("public interface "+ initcap(tablename) +"Mapper extends IMapper<"+ initcap(tablename) +"> {\r\n\r\n}\r\n");
		return sb.toString();
	}
	
	/** 
	 * <p>说明：生成mapper的xml代码</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 10:12:09 AM
	 */ 
	private static String creatMapperXml(){
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n");
		sb.append("<!-- "+ tabCom+ " -->\r\n");
		sb.append("<mapper namespace=\""+pagPathM+"."+ initcap(tablename) +"Mapper\">\r\n\r\n");
		sb.append("\t<!-- 添加 -->\r\n");
		sb.append("\t<insert id=\"insert\" parameterType=\""+ initcap(tablename) +"\">\r\n");
		sb.append("\t\t<selectKey resultType=\"int\" order=\"BEFORE\" keyProperty=\""+ colnames[0].toLowerCase() +"\">\r\n");
		sb.append("\t\t\tSELECT "+tablename+"_SEQ.NEXTVAL FROM DUAL\r\n");
		sb.append("\t\t</selectKey>\r\n");
		sb.append("\t\tINSERT INTO "+ tablename +"("+ getColSql() +")\r\n");
		sb.append("\t\t\t\tVALUES("+ getInsSql() +")\r\n");
		sb.append("\t</insert>\r\n\r\n");
		sb.append("\t<!-- 按条件查询多条（不分页） -->\r\n");
		sb.append("\t<select id=\"findByEntity\" parameterType=\""+ initcap(tablename) +"\" resultType=\""+ initcap(tablename) +"\">\r\n");
		sb.append("\t\tSELECT "+ getColSql() +"\r\n");
		sb.append("\t\t\tFROM "+ tablename +" TAB\r\n");
		sb.append("\t\t\t<where>\r\n"+ getSelSql() +"\t\t\t</where>\r\n");
		sb.append("\t\tORDER BY TAB."+ colnames[0] +" DESC\r\n");
		sb.append("\t</select>\r\n\r\n");
		sb.append("\t<!-- 按ID查询一条 -->\r\n");
		sb.append("\t<select id=\"findById\" parameterType=\"int\" resultType=\""+ initcap(tablename) +"\">\r\n");
		sb.append("\t\tSELECT "+ getColSql() +"\r\n");
		sb.append("\t\t\t\tFROM "+ tablename +" TAB WHERE TAB."+ colnames[0] +" = #{"+ colnames[0].toLowerCase() +"}\r\n");
		sb.append("\t</select>\r\n\r\n");
		sb.append("\t<!-- 按ID删除一条 -->\r\n");
		sb.append("\t<delete id=\"delete\" parameterType=\"int\">\r\n");
		sb.append("\t\tDELETE FROM "+ tablename +" TAB WHERE TAB."+ colnames[0] +" = #{"+ colnames[0].toLowerCase() +"}\r\n");
		sb.append("\t</delete>\r\n\r\n");
		sb.append("\t<!-- 按ID修改 -->\r\n");
		sb.append("\t<update id=\"update\" parameterType=\""+ initcap(tablename) +"\">\r\n");
		sb.append("\t\tUPDATE "+ tablename +" TAB\r\n");
		sb.append("\t\t<set>\r\n"+ getUpdSql() +"\t\t</set>\r\n");
		sb.append("\t\tWHERE TAB."+ colnames[0] +" = #{"+ colnames[0].toLowerCase() +"}\r\n");
		sb.append("\t</update>\r\n\r\n");
		sb.append("\t<!-- 按条件查询多条（分页） -->\r\n");
		sb.append("\t<select id=\"findByEntityOnPage\" parameterType=\""+ initcap(tablename) +"\" resultType=\""+ initcap(tablename) +"\">\r\n");
		sb.append("\t<![CDATA[\r\n");
		sb.append("\tSELECT T.* FROM( SELECT TAB.*,ROWNUM AS RN FROM(\r\n");
		sb.append("\t]]>\r\n");
		sb.append("\t\tSELECT "+ getColSql() +"\r\n");
		sb.append("\t\t\tFROM "+ tablename +" TAB\r\n");
		sb.append("\t\t\t<where>\r\n"+ getSelSql() +"\t\t\t</where>\r\n");
		sb.append("\t\tORDER BY TAB."+ colnames[0] +" DESC\r\n");
		sb.append("\t<![CDATA[\r\n");
		sb.append("\t) TAB WHERE ROWNUM <= #{pageNo} * #{pageSize}) T WHERE RN > (#{pageNo}-1) * #{pageSize}\r\n");
		sb.append("\t]]>\r\n");
		sb.append("\t</select>\r\n\r\n");
		sb.append("\t<!-- 按条件查询总数（用于分页） -->\r\n");
		sb.append("\t<select id=\"findByEntityOnPageCount\" parameterType=\""+ initcap(tablename) +"\" resultType=\"int\">\r\n");
		sb.append("\t\tSELECT COUNT(1) \r\n");
		sb.append("\t\t\tFROM "+ tablename +" TAB\r\n");
		sb.append("\t\t\t<where>\r\n"+ getSelSql() +"\t\t\t</where>\r\n");
		sb.append("\t</select>\r\n\r\n");
		sb.append("</mapper>");
		return sb.toString();
	}
	/**
	 * 按数据库的表结构生成Service
	 * @author wangrong
	 * @time Jul 17, 2012 5:03:52 PM
	 */
	public static String creatService(){
		StringBuffer sb = new StringBuffer();
		sb.append("package "+pagPathS+";\r\n\r\n");
		sb.append(impS+"\r\n\r\n");
		sb.append("import "+ pagPathJ +"."+ initcap(tablename)+";\r\n\r\n");
		sb.append("/**\r\n");
		sb.append(" * @author Automatic generation\r\n");
		sb.append(" * @description "+ tabCom+ "IService\r\n");
		sb.append(" * @company Jinxin Computer Corp.\r\n");
		sb.append(" * @date ").append(new Date()).append("\r\n");
		sb.append(" */\r\n");
		sb.append("public interface I"+ initcap(tablename) +"Service extends IService<"+ initcap(tablename) +"> {\r\n\r\n}\r\n");
		return sb.toString();
	}
	/**
	 * 按数据库的表结构生成Service实现
	 * @author wangrong
	 * @time Jul 17, 2012 5:03:52 PM
	 */
	public static String creatServiceImpl(){
		StringBuffer sb = new StringBuffer();
		sb.append("package "+pagPathSI+";\r\n\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n\r\n");
		sb.append("import org.springframework.stereotype.Service;\r\n\r\n");
		sb.append(impAS+"\r\n\r\n");
		sb.append("import "+ pagPathJ +"."+ initcap(tablename)+";\r\n\r\n");
		sb.append("import "+ pagPathM +"."+ mapperName +";\r\n\r\n");
		sb.append("import "+ pagPathS +"."+ serviceName +";\r\n\r\n");
		sb.append("/**\r\n");
		sb.append(" * @author Automatic generation\r\n");
		sb.append(" * @description "+ tabCom+ "Service\r\n");
		sb.append(" * @company Jinxin Computer Corp.\r\n");
		sb.append(" * @date ").append(new Date()).append("\r\n");
		sb.append(" */\r\n");
		sb.append("@Service\r\n");
		sb.append("public class "+ serviceImplName +" extends AbstractService<"+ entityName +"> "+"implements "+ serviceName +"{\r\n");	
		sb.append("\t@Autowired\r\n");
		sb.append("\tprotected void setMapper("+mapperName +" "+fristLower(mapperName)+") {\r\n");
		sb.append("\t\tthis.mapper = ").append(fristLower(mapperName)).append(";\r\n");
		sb.append("\t}\r\n");
		sb.append("}");
		return sb.toString();
	}
	/**
	 * 按数据库的表结构生成Action
	 * @author wangrong
	 * @time Jul 17, 2012 5:03:52 PM
	 */
	public static String createAction(){
		StringBuffer sb = new StringBuffer();
		sb.append("package "+pagPathWeb+";\r\n\r\n");
		sb.append("import javax.annotation.Resource;\r\n");
		sb.append("import org.apache.commons.logging.Log;\r\n");
		sb.append(impPM).append("\r\n");
		sb.append(impBAction).append("\r\n");
		sb.append(impContextHelper).append("\r\n");
		sb.append("import ").append(pagPathS).append(".").append(serviceName).append(";\r\n");
		sb.append("import ").append(pagPathJ).append(".").append(entityName).append(";\r\n");
		sb.append("\r\n");
		sb.append("/**\r\n");
		sb.append(" * @author Automatic generation\r\n");
		sb.append(" * @description "+ tabCom+ "Action \r\n");
		sb.append(" * @company Jinxin Computer Corp.\r\n");
		sb.append(" * @date ").append(new Date()).append("\r\n");
		sb.append(" */\r\n");
		sb.append("public class "+ actionName +" extends BaseAction {\r\n");
		sb.append("\tprivate static final long serialVersionUID = 1L;\r\n");
		sb.append("\tprivate static final Log logger = ContextHelper.getLogger("+ actionName +".class);\r\n");
		sb.append("\r\n");
		sb.append("\t//------------------------------------------------注入Service--------------------------------------------------------------//\r\n");
		sb.append("\t@Resource\r\n");
		sb.append("\tprivate "+serviceName+" "+fristLower(serviceImplName)+";\r\n");
		sb.append("\r\n");
		
		
		sb.append("\t//------------------------------------------------页面对象-----------------------------------------------------------------//\r\n");
		sb.append("\tprivate PageModel<"+entityName+"> pm = new PageModel<"+entityName+">();\r\n");
		sb.append("\tprivate "+entityName+" "+fristLower(entityName)+";\r\n");
		sb.append("\r\n");
		
		//添加方法
		sb.append("\t/**\r\n");
		sb.append("\t * 添加"+ tabCom +"\r\n");
		sb.append("\t */\r\n");
		sb.append("\tpublic String insert(){\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\t"+fristLower(serviceImplName)+".insert("+fristLower(entityName)+");\r\n");
		sb.append("\t\t\treturn \"SUCCESS\";\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\tlogger.error(\"新增"+ tabCom +"失败\", e);\r\n");
		sb.append("\t\t\treturn ERROR;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n\r\n");
		//删除
		sb.append("\t/**\r\n");
		sb.append("\t * 删除"+ tabCom +"\r\n");
		sb.append("\t */\r\n");
		sb.append("\tpublic String delete(){\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\t"+fristLower(serviceImplName)+".delete("+fristLower(entityName)+");\r\n");
		sb.append("\t\t\treturn \"SUCCESS\";\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\tlogger.error(\"删除"+ tabCom +"失败\", e);\r\n");
		sb.append("\t\t\treturn ERROR;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n\r\n");
		//修改
		sb.append("\t/**\r\n");
		sb.append("\t * 修改"+ tabCom +"\r\n");
		sb.append("\t */\r\n");
		sb.append("\tpublic String update(){\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\t"+fristLower(serviceImplName)+".update("+fristLower(entityName)+");\r\n");
		sb.append("\t\t\treturn \"SUCCESS\";\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\tlogger.error(\"修改"+ tabCom +"失败\", e);\r\n");
		sb.append("\t\t\treturn ERROR;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n\r\n");
		//查询
		sb.append("\t/**\r\n");
		sb.append("\t * 查询"+ tabCom +"\r\n");
		sb.append("\t */\r\n");
		sb.append("\tpublic String query(){\r\n");
		sb.append("\t\ttry {\r\n");
		sb.append("\t\t\tpm = "+fristLower(serviceImplName)+".findOnPage("+fristLower(entityName)+",pm);\r\n");
		sb.append("\t\t\treturn \"SUCCESS\";\r\n");
		sb.append("\t\t} catch (Exception e) {\r\n");
		sb.append("\t\t\tlogger.error(\"查询"+ tabCom +"失败\", e);\r\n");
		sb.append("\t\t\treturn ERROR;\r\n");
		sb.append("\t\t}\r\n");
		sb.append("\t}\r\n");
		sb.append("\r\n");
		
		
		sb.append("\t//------------------------------------------------getter/setter------------------------------------------------------------//\r\n");
		//---set方法---
		sb.append("\tpublic void set" + entityName + "(" + entityName + " " + fristLower(entityName) + "){\r\n");
		sb.append("\t\tthis." + fristLower(entityName) + " = " + fristLower(entityName) + ";\r\n");
		sb.append("\t}\r\n");
		//---get方法-------
		sb.append("\tpublic " + entityName + " get" + entityName + "(){\r\n");
		sb.append("\t\treturn this." + fristLower(entityName) + ";\r\n");
		sb.append("\t}\r\n");
		
		//---set方法---
		sb.append("\tpublic void setPm(PageModel<"+entityName+"> pm){\r\n");
		sb.append("\t\tthis.pm = pm;\r\n");
		sb.append("\t}\r\n");
		//---get方法-------
		sb.append("\tpublic PageModel<" + entityName + "> getPm(){\r\n");
		sb.append("\t\treturn this.pm;\r\n");
		sb.append("\t}\r\n");
		sb.append("}");
		return sb.toString();
	}
	/** 
	 * <p>说明：生成实体类java代码</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 9:21:07 AM
	 */ 
	private static String parse() {
		StringBuffer sb = new StringBuffer();
		sb.append("package "+pagPathJ+";\r\n\r\n");
		if (f_util) {
			sb.append("import java.util.Date;\r\n");
		}
		if (f_sql) {
			sb.append("import java.sql.*;\r\n\r\n\r\n");
		}
		if (f_ext_entity&&!StringUtils.isEmpty(imp_entity)) {
			sb.append(imp_entity).append("\r\n\r\n");
		}
		sb.append("/**\r\n").append(" * " + tabCom + "\r\n").append(" */\r\n")
				.append("public class ").append(initcap(tablename));
		if (f_ext_entity) {
			sb.append(" extends Entity");
		}
		sb.append(" {\r\n");
		processAllAttrs(sb);
		sb.append("\r\n\r\n");
		processAllMethod(sb);
		sb.append("}\r\n");
		return sb.toString();
	}

	/** 
	 * <p>说明：生成类方法</p>
	 * @param sb
	 * @return void
	 * @author tuxy
	 * @date Jul 17, 2012 5:58:28 PM
	 */ 
	private static void processAllMethod(StringBuffer sb) {
		for (int i = 0; i < colnames.length; i++) {
			//---set方法---
			sb.append("\t/**\r\n");
			sb.append("\t * "+ colComs[i] +"\r\n");
			sb.append("\t */\r\n");
			sb.append("\tpublic void set" + initcap2(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " "
					+ colnames[i].toLowerCase() + "){\r\n");
			sb.append("\t\tthis." + colnames[i].toLowerCase() + " = " + colnames[i].toLowerCase() + ";\r\n");
			sb.append("\t}\r\n");
			//---get方法-------
			sb.append("\t/**\r\n");
			sb.append("\t * "+ colComs[i] +"\r\n");
			sb.append("\t */\r\n");
			sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap2(colnames[i]) + "(){\r\n");
			sb.append("\t\treturn " + colnames[i].toLowerCase() + ";\r\n");
			sb.append("\t}\r\n");
		}
	}

	/** 
	 * <p>说明：生成类属性</p>
	 * @param sb
	 * @return void
	 * @author tuxy
	 * @date Jul 17, 2012 5:58:03 PM
	 */ 
	private static void processAllAttrs(StringBuffer sb) {
		for (int i = 0; i < colnames.length; i++) {
			sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i].toLowerCase() + ";  //"+ colComs[i] +"\r\n");
		}
	}

	/** 
	 * <p>说明：首字母大写，下划线去掉，下划线后一字母大写</p>
	 * @param str
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 17, 2012 5:55:00 PM
	 */ 
	private static String initcap(String str) {
		char[] ch = str.toLowerCase().toCharArray();
		if (ch[0] >= 'a' && ch[0] <= 'z') {
			ch[0] = (char) (ch[0] - 32);
		}
		for(int i=1;i<ch.length;i++){
			if(ch[i]=='_'){
				if (ch[i+1] >= 'a' && ch[i+1] <= 'z') {
					ch[i+1] = (char) (ch[i+1] - 32);
				}
			}
		}
		return new String(ch).replace("_", "");
	}
	
	private static String fristLower(String str){
		if(str==null||str.equals("")){
			return "";
		}else{
			return str.substring(0,1).toLowerCase()+str.substring(1);
		}
	}
	
	/** 
	 * <p>说明：下划线去掉，下划线后一字母大写</p>
	 * @param str
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 17, 2012 8:19:26 PM
	 */ 
	private static String initcap2(String str) {
		char[] ch = str.toLowerCase().toCharArray();
		if (ch[0] >= 'a' && ch[0] <= 'z') {
			ch[0] = (char) (ch[0] - 32);
		}
		return new String(ch);
	}

	/** 
	 * <p>说明：oracle数据类型转换为java数据类型</p>
	 * @param sqlType
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 17, 2012 5:56:01 PM
	 */ 
	private static String sqlType2JavaType(String sqlType) {
		if (sqlType.equalsIgnoreCase("number")) {
			return "Integer";
		}else if(sqlType.equalsIgnoreCase("date")){
			return f_date_to_str?"String":"Date";
		}else if (sqlType.equalsIgnoreCase("varchar2")||sqlType.equalsIgnoreCase("char")) {
			return "String";
		} 
		return null;
	}
	
	/** 
	 * <p>说明：把列数组转换为sql列</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 11:12:57 AM
	 */ 
	private static String getColSql() {
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<colnames.length;i++){
			if(i>0){
				sb.append(",");
			}
			sb.append(colnames[i]);
		}
		return sb.toString();
	}
	
	/** 
	 * <p>说明：把列数组转换为添加的参数sql</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 11:25:52 AM
	 */ 
	private static String getInsSql(){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<colnames.length;i++){
			if(i>0){
				sb.append(",");
			}
			sb.append("#{"+ colnames[i].toLowerCase() +"}");
		}
		return sb.toString();
	}
	
	/** 
	 * <p>说明：把列数组转换为查询的条件sql</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 11:25:52 AM
	 */ 
	private static String getSelSql(){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<colnames.length;i++){
			sb.append("\t\t\t\t<if test=\""+ colnames[i].toLowerCase() +"!=null and "+ colnames[i].toLowerCase() +"!='' \">AND TAB."+ colnames[i] +" = #{"+ colnames[i].toLowerCase() +"}</if>\r\n");
		}
		return sb.toString();
	}
	
	/** 
	 * <p>说明：把列数组转换为修改的条件sql</p>
	 * @return
	 * @return String
	 * @author tuxy
	 * @date Jul 18, 2012 11:25:52 AM
	 */ 
	private static String getUpdSql(){
		StringBuffer sb = new StringBuffer();
		for(int i=1;i<colnames.length;i++){ //从第二条开始，id不会被修改
			sb.append("\t\t\t<if test=\""+ colnames[i].toLowerCase() +"!=null and "+ colnames[i].toLowerCase() +"!='' \">TAB."+ colnames[i] +" = #{"+ colnames[i].toLowerCase() +"},</if>\r\n");
		}
		return sb.toString();
	}
	
	public static Connection getConnJdbc_161()throws Exception{
		Connection conn = tl.get();
		if(conn == null){
			Class.forName(CONN_DRIVER_CLASS);
			conn = DriverManager.getConnection(CONN_URL,CONN_USER,CONN_PASSWORD);
			tl.set(conn);
		}
		return conn;
	}
	
	/**
	 * 系统调用的链接方法
	 * @return
	 * @throws Exception
	 */
	public static Connection getConn()throws Exception{
		return getConnJdbc_161();
	}
	
	/**
	 * 释放资源
	 */
	public static void release(Statement pstm, ResultSet rs , Connection conn){
       if( pstm != null ) try{ pstm.close();}catch( Exception e ){e.printStackTrace();}
       if( rs != null ) try{ rs.close(); }catch( Exception e ){e.printStackTrace();}
       if( conn != null ) try{ conn.close(); tl.remove(); }catch(Exception e ){ e.printStackTrace(); } 
    }
}
